package cn.uce.cc.srv.biz.impl;

import cn.uce.base.exception.BusinessException;
import cn.uce.cc.api.service.biz.IWaybillBiz;
import cn.uce.cc.srv.utils.HttpClientUtil;
import cn.uce.common.constants.Constants;
import cn.uce.ics.base.vo.cs.CsWoCustSerVo;
import cn.uce.service.biz.ICcWaybillQueryBiz;
import cn.uce.utils.StringUtil;
import cn.uce.web.authorg.dao.IDictDataDao;
import cn.uce.web.authorg.vo.DictDataVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("waybillBiz")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class WaybillBiz  implements IWaybillBiz {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private ICcWaybillQueryBiz ccWaybillQueryBiz;

    @Resource
    private IDictDataDao omgSyncDictDataDao;

    @Override
    public Map<String, Object> findTraceInfo(String billCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            DictDataVo vo = new DictDataVo();
            vo.setTypeClassCode("ICS_URL");
            vo.setTypeId(1);
            DictDataVo resultVo = omgSyncDictDataDao.findOneByCondition(vo);
            if(resultVo != null){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(Constants.BILL_CODE, billCode);
                String map = HttpClientUtil.doPostJson(resultVo.getTypeName(), jsonObject, true);
                logger.info("findtracInfoNew318" + JSON.toJSONString(map));
                if (StringUtil.isNotEmpty(map)) {
                    Map<String, Object> reMap = JSONObject.parseObject(map);
                    if (reMap.get("code") != null) {
                        if ((int) reMap.get("code") == 0) {//成功
                            List<Map> re = JSONArray.parseArray(String.valueOf(reMap.get("date")), Map.class);
                            if (re.size() > 0) {
                                result = re.get(0);
                            }
                        } else if ((int) reMap.get("code") == 1) {
                            throw new BusinessException("运单轨迹信息查询接口：参数为空————" + reMap.get(Constants.MESSAGE));
                        } else {
                            throw new BusinessException("运单轨迹信息查询接口：参数不符规则————" + reMap.get(Constants.MESSAGE));
                        }
                    }
                }
            }
            if(result != null){
                logger.info(JSONObject.toJSONString(result));
            }
        }catch (Exception e){
            logger.error("接口请求异常",e);
        }
        return result;
    }

    @Override
    public Map<String, Object> findProblemInfo(CsWoCustSerVo csWoCustSerVo) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = ccWaybillQueryBiz.findProblemInfo(csWoCustSerVo);
        }catch (Exception e){
            logger.error("接口请求异常",e);
        }
        return result;
    }

    @Override
    public Map<String, Object> findWaybllInfo(String billCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = ccWaybillQueryBiz.findWaybllInfo(billCode);
        }catch (Exception e){
            logger.error("接口请求异常",e);
        }
        return result;
    }

    @Override
    public Map<String, Object> findDispenseList(String billCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = ccWaybillQueryBiz.findDispenseList(billCode);
        }catch (Exception e){
            logger.error("接口请求异常",e);
        }
        return result;
    }

    @Override
    public Map<String, Object> findChildWaybllInfo(String billCode) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = ccWaybillQueryBiz.findChildWaybllInfo(billCode);
        }catch (Exception e){
            logger.error("接口请求异常",e);
        }
        return result;
    }

    public static void main(String[] args) {
        String json = "{'date':[{'scan':[{'createDate':'2020-04-24','createTime':'10:53:56','scanType':'收件','trackMessage':'快件已由【<font color='FF0000' style='font-weight:bold'>虎门</font>】的伍国洪收件'}]}],'message':'','trace':{'message':'','code':0,'date':[{'sign':[],'scan':[{'createTime':1587696792550,'scanDataId':276102,'createOrg':10003551,'scanTime':1587696836045,'scanCenterCode':'UC000164','scanAccOrg':10003551,'scanDataCode':'00012432','scanType':1,'createEmp':'邹锡彪测试虎门','scanOrgId':10003551,'scanOrgName':'虎门','scanAccOrgCode':'UC003303','scanEmpId':10206383,'scanEmpCode':'00012351','scanEmpName':'邹锡彪测试虎门','billCode':'889518104010109','scanDataName':'伍国洪','deleteFlag':false,'updateEmp':'邹锡彪测试虎门','scanDataIdCode':'00012432','scanOrgCode':'UC003303'}]}]},'code':0}'{'date':[{'scan':[{'createDate':'2020-04-06','createTime':'04:32:42','scanType':'收件','trackMessage':'快件已由【<font color='FF0000' style='font-weight:bold'>广园一1</font>】的陈牛牛收件'},{'createDate':'2020-04-06','createTime':'04:32:42','scanType':'派件','trackMessage':'【<font color='FF0000' style='font-weight:bold'>广园一1</font>】正在派件,派件员:陈牛牛'}]}],'message':'','trace':{'message':'','code':0,'date':[{'sign':[],'scan':[{'createTime':1586118763129,'scanDataId':411205,'createOrg':10003561,'scanTime':1586118762912,'scanCenterCode':'UC000164','scanAccOrg':10003561,'scanDataCode':'00013133','scanType':1,'createEmp':'陈牛牛','scanOrgId':10003561,'scanOrgName':'广园一1','scanAccOrgCode':'UC003313','scanEmpId':411205,'scanEmpCode':'00013133','scanEmpName':'陈牛牛','billCode':'889900700180270','scanDataName':'陈牛牛','deleteFlag':false,'updateEmp':'陈牛牛','scanDataIdCode':'00013133','scanOrgCode':'UC003313'},{'createTime':1586118763197,'scanDataId':411205,'createOrg':10003561,'scanTime':1586118762000,'scanCenterCode':'UC000164','scanAccOrg':10003561,'scanDataCode':'00013133','scanType':4,'createEmp':'陈牛牛','scanOrgId':10003561,'scanOrgName':'广园一1','scanAccOrgCode':'UC003313','scanEmpId':411205,'scanEmpCode':'00013133','scanEmpName':'陈牛牛','billCode':'889900700180270','scanDataName':'陈牛牛','deleteFlag':false,'updateEmp':'陈牛牛','scanDataIdCode':'00013133','scanOrgCode':'UC003313'}]}]},'code':0}";
//        Map<String, Object> reMap = JSONObject.parseObject(json);
//        List<Map> re = JSONArray.parseArray(String.valueOf(reMap.get("date")), Map.class);
       String re =  StringEscapeUtils.escapeJson(json);
        System.out.println(JSONObject.toJSONString(re));
    }
}
